package com.ibm.ops.persistent;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import au.com.bytecode.opencsv.CSVWriter;

import com.ibm.ops.OpstoolConstant;
import com.ibm.ops.domain.metadata.OpsAuditLogReportMetaData;
import com.ibm.ops.exception.PersistentIOException;

public class CSVPersistentHandler implements PersistentHandler {

	private String destination_path = "";
	
//	private List<?> list = null;
	
	public CSVPersistentHandler(String filePath){
		this.destination_path = filePath;
//		this.list = persistentContent;
	}

//	@Override
	public void persistent(List<OpsAuditLogReportMetaData> persistentContent) throws PersistentIOException {
		CSVWriter writer = null;
		if(persistentContent != null){
			try {
				writer = new CSVWriter(new FileWriter(new File(this.destination_path + OpstoolConstant.CSV_SUFFIX)), ',');
				int count = persistentContent.size();
				for(int i = 0; i < count; i++){
					if(i == 0){
						writer.writeNext(persistentContent.get(i).allFieldName());
					}
					writer.writeNext(persistentContent.get(i).toArray());
				}		    
				writer.close();
			} catch (IOException e) {
				throw new PersistentIOException();
			}
		}				
	}


}
